package bo;

import IBO.IClient;
import IBO.IIntervention;
import boerrors.AdresseMinimumMultiplicityException;
import boerrors.ClientInexistantException;
import boerrors.CodePostalMinimumMultiplicityException;
import boerrors.ZoneMinimumMultiplicityException;
import dao.dto.AdresseDto;
import dao.dto.ClientParticulierDto;
import dao.dto.CodePostalDto;
import dao.dto.ZoneDto;
import java.rmi.RemoteException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Client extends Personne implements IClient, Cloneable{
        private int _idClient;
        private String _numeroTva;
        private Vector<Intervention> _listeIntervention;
    
	//*******************************************************************
	//Constructeurs
	//*******************************************************************
        
        public Client(String nom, String prenom, String telephone, String email, int idClient, String numeroTva) throws RemoteException {
            super(nom, prenom, telephone, email);
            setIdClient(idClient);
            setNumeroTva(numeroTva);
            this._listeIntervention = new Vector<Intervention>();
            ListeClients.getInstance().ajouterClient(this);
        }
        
        public Client(  String nom, String prenom, Adresse adresse, String telephone, String email, 
                        int idClient, String numeroTVA) throws RemoteException {
                this(nom, prenom, adresse, telephone, email, numeroTVA);
                setIdClient(idClient);
                this._listeIntervention = new Vector<Intervention>();
                ListeClients.getInstance().ajouterClient(this);                
        }
    
        public Client(  String nom, String prenom, Adresse adresse, String telephone, String email, 
                        int idClient)  throws RemoteException {
                this(nom, prenom, adresse, telephone, email);
                setIdClient(idClient);
                this._listeIntervention = new Vector<Intervention>();
                ListeClients.getInstance().ajouterClient(this);                
        }
    
        public Client(  String nom, String prenom, Adresse adresse, String telephone, String email, 
                        String numeroTVA) throws RemoteException {
                super(nom, prenom, adresse, telephone, email);
                setNumeroTva(numeroTVA);
                this._listeIntervention = new Vector<Intervention>();
                ListeClients.getInstance().ajouterClient(this);                
        }    
    
        public Client(String nom, String prenom, Adresse adresse, String telephone, String email) throws RemoteException {
                super(nom, prenom, adresse, telephone, email);
                this._listeIntervention = new Vector<Intervention>();
                ListeClients.getInstance().ajouterClient(this);
        }
    
	//*******************************************************************
	//Setters
	//*******************************************************************
        public synchronized void setIdClient(int idClient) {
                this._idClient = idClient;
        }

        public synchronized void setNumeroTva(String numeroTva) {
                this._numeroTva = numeroTva;
        }    
    
	//*******************************************************************
	//Getters
	//*******************************************************************
        public synchronized int getIdClient() throws RemoteException  {
                return _idClient;
        }
        
        public synchronized String getNumeroTva() throws RemoteException {
                return _numeroTva;
        }

        public synchronized Vector<IIntervention> getListeInterventions() throws RemoteException {
            Vector<IIntervention> listeTampon = new Vector<IIntervention>();
            for (int i = 0; i < this._listeIntervention.size(); i++){
                listeTampon.add((IIntervention) this._listeIntervention.get(i));
            }
            return listeTampon;
        }
        
	//*******************************************************************
	//Associations
	//*******************************************************************
        public synchronized boolean ajouterIntervention(Intervention intervention) throws RemoteException {
                if(intervention==null)return false;
                if(this.contientIntervention(intervention))return false;
                if(!this.peutAjouterIntervention())return false;
                try{    
                    if(intervention.peutEnregistrerClient() && intervention.getClient()!=this)return false;
                } catch (RemoteException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClientInexistantException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);}
                _listeIntervention.add(intervention);
                intervention.enregistrerClient(this);
                //_listeIntervention.addElement(intervention);
                return true;
        }
    
        public synchronized boolean supprimerIntervention(Intervention intervention) throws RemoteException {
                if(intervention==null)return false;
                if(!this.contientIntervention(intervention))return false;
                _listeIntervention.remove(intervention);
                intervention.supprimerTechnicien();
                return true;
        }
    
        public boolean peutAjouterIntervention(){
                return true;
        }
    
        public boolean contientIntervention(Intervention intervention) throws RemoteException{
                //return _listeIntervention.contains(intervention);
                for (int i=0 ; i < this.getListeInterventions().size() ; i++)
                    if (this.getListeInterventions().get(i).getIdIntervention() == intervention.getIdIntervention())
                    return true;
                return false;
        }
                
        //*******************************************************************
	//Method Clonable
	//*******************************************************************        
        
        @Override
        public Object clone(){
            // On récupère l'instance à renvoyer par l'appel de la 
	    // méthode super.clone() (ici : Personne.clone())
             Client client = (Client) super.clone();
            // on renvoie le clone
            return client;
        }        
}